package sdk.org.apache.shiro.G2_使用.C05_认证_3重试次数;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import com.titan.core.utils.data.StringUtils;

import sdk.org.apache.shiro.utils.ShiroUtils;


public class UserRealm extends AuthorizingRealm {
    private final static Map<String,String> users = new HashMap<String,String>();
    
    public void putAccount(String username, String password) {
        users.put(username, password);
    }
    
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }
    
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = ShiroUtils.getUsername(token);
        String password = users.get(username);
        password = ShiroUtils.getPassword(username, password);
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                username,  // 用户名
                password,  // 密码
                ByteSource.Util.bytes(username),
                getName()  // realm name
        );
        return authenticationInfo;
    }
}
